package com.c.ctgproject.ui.collect.check

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.*
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityOutCheckDetailBinding
import com.c.ctgproject.entity.guang.CheckDetailEntity
import com.c.ctgproject.entity.guang.MaterialOutCheckDetailEntity
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.c.ctgproject.wiget.common.CarInfoLayout
import com.c.ctgproject.wiget.common.CommonInputDialog
import com.c.ctgproject.wiget.dialog.CheckInputNameDialog
import org.greenrobot.eventbus.EventBus

/**
 *作者:LiBW
 *创建日期: 2021/10/15
 *描述: 出库审批详情 页面
 */
class OutCheckDetailActivity : BaseActivityV2(), CommonInputDialog.CommonInPutDialogCallBack, CheckInputNameDialog.CheckInputNameDialogCallback {
    private val mBinding by lazy { ActivityOutCheckDetailBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private val outOrderId by lazy { intent.getIntExtra(MyConstants.ID, -1) }


    //物品明细
//    private val mPartList = ArrayList<MaterialOutCheckDetailEntity.DataBean.OutItemsBean>()
//    private val mPartAdapter by lazy { OutDetailForCheckAdapter(this, mPartList) }
    //流程
    private val mFlowList = ArrayList<MaterialOutCheckDetailEntity.DataBean.HistoriesBean>()
    private val mFlowAdapter by lazy { OutCheckFlowAdapter(this, mFlowList) }

    private val mListPosition by lazy { intent.getIntExtra(MyConstants.POSITION,-1) }

    override fun event() {
        if (intent.getBooleanExtra(MyConstants.JUST_SHOW, false)) {
            mBinding.mBottomRL.visibility = View.GONE
        }
        ApiMethods.apiSubscribe(ApiMethods.retrofit.outMaterialDetail(outOrderId), MyObserverHandlerLoading(this, loading) {
            MyLog.d("详情回调:$it")
            val data = getGson().fromJson(it, MaterialOutCheckDetailEntity::class.java).data
            data?.let { bean ->
                mBinding.mContainerLL.addView(CarInfoLayout(this, "出库单号", bean.bizNo.noDataHandler()))
                val outType = when (bean.outType) {
                    1 -> "销售出库"
                    2 -> "借用出库"
                    3 -> "其他出库"
                    else -> "暂无数据"
                }
                mBinding.mContainerLL.addView(CarInfoLayout(this, "出库类型", outType))
                mBinding.mContainerLL.addView(CarInfoLayout(this, "客户", bean.customer.noDataHandler()))
                mBinding.mContainerLL.addView(CarInfoLayout(this, "开单人", bean.createPersonName.noDataHandler()))
                mBinding.mContainerLL.addView(CarInfoLayout(this, "开单时间", bean.createTime.noDataHandler()))
                mBinding.mContainerLL.addView(CarInfoLayout(this, "审批人", bean.auditPersonName.noDataHandler()))
                mBinding.mContainerLL.addView(CarInfoLayout(this, "备注", bean.remark.noDataHandler()))
            }

            //出库方式 1-电池 2-零件 3-物料
            when(data?.outMethod){
                1->{
                    mBinding.detailTV.text="电池明细"
                    //电池明细
                    data.batteryItems?.let {list->
                        val mPartAdapter = OutBatteryDetailForCheckAdapter(this, list)
                        mBinding.mPartListRV.adapter = mPartAdapter
                    }
                }
                2->{
                    mBinding.detailTV.text="零部件"
                    //零部件明细
                    data.partsItems?.let {list ->
                        val mPartAdapter = OutPartDetailAdapter(this, list)
                        mBinding.mPartListRV.adapter = mPartAdapter
                    }
                }
                3->{
                    //出库物品明细
                    data.materialItems?.let { list ->
                        mBinding.detailTV.text="物品明细"
                        val mPartAdapter = OutDetailForCheckAdapter(this, list)
                        mBinding.mPartListRV.adapter = mPartAdapter
                    }
                }
            }

            //流程
            data?.histories?.let { list ->
                mFlowList.addAll(list)
                mFlowAdapter.notifyDataSetChanged()
            }
        })

        mBinding.mFlowRV.adapter = mFlowAdapter
        mBinding.mFlowRV.layoutManager = LinearLayoutManager(this)

        mBinding.mPartListRV.layoutManager = LinearLayoutManager(this)
    }

    //拒绝理由
    private var inputDialog: CommonInputDialog? = null
    override fun click() {
        //拒绝
        mBinding.mRefusedBTN.setOnClickListener {
            inputDialog = CommonInputDialog(this, "拒绝理由")
            inputDialog?.show(supportFragmentManager, "")
        }
        //同意
        mBinding.mConfirmBTN.setOnClickListener {
            CheckInputNameDialog(loading, this).show(supportFragmentManager, "")

        }
        //物品明细折叠开关
        mBinding.mPartListLL.setOnClickListener {
            if (mBinding.mPartListRV.isVisible) {
                mBinding.mArrowIV.handlerArrow(false, mBinding.mPartListRV)
            } else {
                mBinding.mArrowIV.handlerArrow(true, mBinding.mPartListRV)
            }
        }
    }

    override fun dialogResultText(msg: String) {
        inputDialog?.dismiss()
        handlerHttp(2, msg = msg)
    }

    override fun onImageGet(url: String) {
        handlerHttp(1, nameUrl = url)
    }

    //1-通过 2-不通过
    private fun handlerHttp(status: Int, msg: String? = null, nameUrl: String? = null) {
//        val map = hashMapOf<String, Any?>()
//        map["status"] = status
//        if (status == 0) {
//            map["msg"] = msg
//        }
        ApiMethods.apiSubscribe(ApiMethods.retrofit.outCheckSubmit(outOrderId, status, msg, nameUrl), MyObserverHandlerLoading(this, loading) {
            MyLog.d("回调回调:$it")
            handlerHttpResult(it) {
                showToast("提交成功")
                EventBus.getDefault().post(PositionEvent(mListPosition))
                finish()
            }
        })
    }

}